package action;

import conexao.FabricaDeConexao;
import dao.JDBCOpcoesMenuDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.OpcoesMenu;

public class InsereOpcoesMenu extends HttpServlet {

  private static final long serialVersionUID = 1L;
    JDBCOpcoesMenuDao JDBCOpcoes;
    FabricaDeConexao conecta = new FabricaDeConexao();
    Connection conexao;
    Statement st;
    ResultSet rs;
    String query;

    public class AdicionaUsuarioServlet extends HttpServlet {

        /**
         *
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            PrintWriter out = response.getWriter();
            String nome = request.getParameter("txtBusca");

            OpcoesMenu novoMenu = new OpcoesMenu();
            novoMenu.setNomePrato("txtBusca");
            novoMenu.setPrecoPrato("txtBusca");
            novoMenu.setDescricao("txtBusca");

            JDBCOpcoesMenuDao dao = new JDBCOpcoesMenuDao(conecta, conexao, null, null, query);
            dao.listar();

            out.println("<html>");
            out.println("<body>");
            out.println("O usuário: " + novoMenu.getNomePrato() + " foi cadastrado com sucesso.");
            out.println("</body>");
            out.println("</html>");
        }

        @Override
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
        }

        @Override
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {

            String nomePrato = request.getParameter("nomePrato");
            String precoPrato = request.getParameter("precoPrato");
            String descricaoPrato = request.getParameter("descricaoPrato");

            OpcoesMenu novaOpcao = new OpcoesMenu();
            novaOpcao.setNomePrato(nomePrato);
            novaOpcao.setPrecoPrato(precoPrato);
            novaOpcao.setDescricao(descricaoPrato);

            FabricaDeConexao fabrica = new FabricaDeConexao();
            Connection conexao = fabrica.fazConexao();

            JDBCOpcoesMenuDao bean = new JDBCOpcoesMenuDao(conecta, conexao, null, null, query);
            bean.insereOpcoesMenu(novaOpcao);

            System.out.println("DEU CERTO");
            System.out.println("JESUS É A NOSSA FORÇA.");

            response.sendRedirect("http://localhost:8080/EquipeTaiti/faces/ConfirmaCadNovoPrato.xhtml");

            fabrica.fazConexao();
        }
    }
}
